package com.jf.test.mvvm

import android.util.Log
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.fawvw.vehice.netlib.entity.HttpEvent
import com.fawvw.vehice.netlib.entity.RespEntity
import com.fawvw.vehice.netlib.retrofit.ReqCallback
import com.jf.test.mvvm.base.BasePresenter
import com.jf.test.mvvm.base.dataConvert
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * @Class: MainAcPresenter
 * @Description:
 * @author: github.com/jackyflame
 * @Date: 2021/3/12
 */
class MainAcPresenter : BasePresenter {

    var info: MutableLiveData<String>

    constructor(lifecycleOwner: LifecycleOwner, vm: ViewModel) : super(lifecycleOwner, vm) {
        this.info = MutableLiveData<String>()
    }

    private val apiService:MainAcService by lazy { createService(MainAcService::class.java) }

    suspend fun <T> getDataX(apiUrl:String,params:Map<String,Any>,callback:ReqCallback<T>?){
        vm.viewModelScope.launch {
            callback?.onReqStart()
            var data:RespEntity<T> = withContext(Dispatchers.IO){
                apiService.httpGet(apiUrl,params);
            }
            if(data.isSucceed){
                callback?.onNetResp(data.data);
            }else{
                callback?.onReqError(HttpEvent(data.code,data.errorMessage))
            }
        }
        apiService.httpGet<T>(apiUrl,params)
    }

    suspend fun <T> postDataX(apiUrl:String,params:Map<String,Any>,callback:ReqCallback<T>?){
        vm.viewModelScope.launch {
            callback?.onReqStart()
            var data:RespEntity<T> = withContext(Dispatchers.IO){
                apiService.httpGet(apiUrl,params);
            }
            if(data.isSucceed){
                callback?.onNetResp(data.data);
            }else{
                callback?.onReqError(HttpEvent(data.code,data.errorMessage))
            }
        }
        apiService.httpGet<T>(apiUrl,params)
    }

    fun test(){
        vm.viewModelScope.launch {
            try {
                var params = HashMap<String,Any>()
                var data = withContext(Dispatchers.IO){
                    apiService.getHistory(params).dataConvert();
                }
                //info.value = data;
            }catch (e:Exception){
                e.printStackTrace()
            }
        }
    }

    fun getHistory(){
        var callback = object:ReqCallback<List<SearchHistoryVo>> {
            override fun onReqStart() {

            }
            override fun onNetResp(rst: List<SearchHistoryVo>?) {
                Log.d("MainAcPresenter","getHistory SUCCESS >>> ${rst?.size}")
            }
            override fun onReqError(error: HttpEvent?) {
                Log.d("MainAcPresenter","getHistory onReqError >>> ${error?.code}:${error?.message}")
            }
        }
        request({
            apiService.getHistory(null)
        },callback)
    }

}